package com.example.mypplication.doubleslide

import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mypplication.R


class BiDemoActivity : AppCompatActivity() {
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
//        setContentView(R.layout.activity_bi_demo)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
//    }


    private var scrollTable: TableLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bi_demo)

        scrollTable = findViewById<TableLayout>(R.id.scrollTable)
        // 生成100行×20列的表格数据（确保超出屏幕）
        generateLargeTableData(100, 20)
    }

    /**
     * 生成大量表格数据，确保横竖方向均超过屏幕
     * @param rowCount 行数（足够多，让高度超出屏幕）
     * @param colCount 列数（足够多，让宽度超出屏幕）
     */
    private fun generateLargeTableData(rowCount: Int, colCount: Int) {
        // 先添加表头
        val headerRow = TableRow(this)
        headerRow.setBackgroundColor(Color.LTGRAY)
        for (col in 0 until colCount) {
            val headerCell = createTableCell("表头 " + (col + 1), true)
            headerRow.addView(headerCell)
        }
        scrollTable!!.addView(headerRow)

        // 再添加数据行
        for (row in 0 until rowCount) {
            val dataRow = TableRow(this)
            // 隔行变色，便于区分
            dataRow.setBackgroundColor(if (row % 2 == 0) Color.WHITE else Color.parseColor("#f9f9f9"))

            for (col in 0 until colCount) {
                val dataCell = createTableCell("行" + (row + 1) + "列" + (col + 1), false)
                dataRow.addView(dataCell)
            }
            scrollTable!!.addView(dataRow)
        }
    }

    /**
     * 创建表格单元格
     * @param text 单元格文本
     * @param isHeader 是否为表头
     * @return 带样式的TextView
     */
    private fun createTableCell(text: String, isHeader: Boolean): TextView {
        val cell = TextView(this)
        cell.text = text
        cell.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
        cell.setPadding(20, 20, 20, 20) // 增加内边距，让单元格更宽更高
        cell.setTextColor(if (isHeader) Color.BLACK else Color.DKGRAY)
        cell.isSingleLine = true // 强制单行，避免文本换行导致宽度不足
        return cell
    }
}